﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using prufa.ViewModels;
using prufa.Models;

namespace prufa.Controllers
{
    //Make a UserController that inherits from the Controller.ASG&SAS
    public class UserController : Controller
    {
        //make new instance of UserContext .ASG
        DefaultConnection db = new DefaultConnection();
        //
        // GET: /User/

        //the action transfers the user-list immediately to the view. ASG
        
        public ActionResult UserList()
        {
            //make user variable that contains the userProfiles and makes a list. ASG
            var user = db.UserProfiles.OrderBy(model => model.UserName).ToList();
            
            return View(user);
        }

        //the action transfers the user immediately to the view. ASG
        public ActionResult UserId(int id)
        {
            GameRepository gr = new GameRepository();
            IEnumerable<Game> gamename = gr.GetAllGames();
            HighScoreRepository hr = new HighScoreRepository();
            UserProfile user = db.UserProfiles.Find(id);
            IEnumerable<HighScore> score = hr.FindHighScoreFromUserId(id);
            int ratio = hr.WinRatio(id);
            if (ratio <= 0)
            {
                ratio = 0;
            }
           
            //UserProfiles.Find(id); 
            return View(new UserAndHighScore { HighScore = score, UserProfile = user, GameName = gamename, WinRatio = ratio });
        }
        
        [Authorize(Roles="Admin")]
        public ActionResult AdminUserList()
        {
            //make user variable that contains the userProfiles and makes a list. ASG
            var user = db.UserProfiles.OrderBy(model => model.UserName).ToList();

            return View(user);
        }

        // GET
        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id)
        {
            UserProfile user = this.db.UserProfiles.Find(id);

            if (user == null)
            {
                return this.HttpNotFound();
            }

            return this.View(user);
        }

        // POST
        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            UserProfile user = this.db.UserProfiles.Find(id);
            this.db.UserProfiles.Remove(user);
            this.db.SaveChanges();

            return RedirectToAction("AdminUserList");
        }

        // GET
        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            UserProfile user = this.db.UserProfiles.Find(id);

            if (user == null)
            {
                return this.HttpNotFound();
            }

            return this.View(user);
        }

        // POST
        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult Edit(UserProfile user)
        {
            if (ModelState.IsValid)
            {
                UserProfile dbUser = new UserProfile { UserId = user.UserId };
                db.UserProfiles.Attach(dbUser);

                dbUser.UserName = user.UserName;
                dbUser.Email = user.Email;
                dbUser.IsBanned = user.IsBanned;
                dbUser.IsAdmin = user.IsAdmin;
                dbUser.Avatar = user.Avatar;
                dbUser.Date = user.Date;

                db.SaveChanges();

                return this.RedirectToAction("AdminUserList");
            }

            return this.View(user);
        }

        // GET
        public ActionResult ChangeAvatar()
        {
            UserProfile user = this.db.UserProfiles.Where(u => u.UserName == User.Identity.Name).First();

            if (user == null)
            {
                return this.HttpNotFound();
            }

            return this.View(user);
        }

        // POST
        [HttpPost]
        public ActionResult ChangeAvatar(UserProfile user)
        {
            if (ModelState.IsValid)
            {
                UserProfile dbUser = new UserProfile { UserId = user.UserId };
                db.UserProfiles.Attach(dbUser);

                dbUser.UserName = user.UserName;
                dbUser.Email = user.Email;
                dbUser.IsBanned = user.IsBanned;
                dbUser.IsAdmin = user.IsAdmin;
                dbUser.Avatar = user.Avatar;
                dbUser.Date = user.Date;

                db.SaveChanges();

                return this.RedirectToAction("UserId", new { id = user.UserId });
            }

            return this.View(user);
        }
    }
}
